<?php

namespace App\Console\Commands\Transfer;

use App\Models\Ca\Docker;
use App\Models\Ca\Visit;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class DockerCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'transfer:docker';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '迁移';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $data = DB::connection('S')->table('common_admin as m')
            ->join('common_admin_group as cag', 'cag.admin_id', '=', 'm.id')
            ->join('common_group_action as cga', 'cga.group_id', '=', 'cag.group_id')
            ->join('common_actions as ca', 'ca.id', '=', 'cga.action_id')
            ->join('bs_user_hospital as buh', 'm.id', '=', 'buh.user_id')
            ->where('ca.code', '=', 'can_be_selected_as_doctor')->get([
                'm.id',
                'm.name',
                'buh.hospital_id',
            ]);

        foreach ($data as $datum) {
            $datum->hospital_id = $datum->hospital_id < 0 ? 0 : $datum->hospital_id;
            if (!Docker::query()->where('id', '=', $datum->id)->exists())
                Docker::create((array)$datum);
        }
    }
}